import { CollectionRecordModel } from '@/models/collectionRecord'
import CollectionRecorTable from '@/pages/CollectionRecord/components/CollectionRecordTable'
import CollectionRecorUpdateModal from '@/pages/CollectionRecord/components/CollectionRecordUpdateModal'
import { CollectionRecordVO, saveCollectionRecord, updateCollectionRecord } from '@/services/CollectionRecordService'
import { useSnapshot } from '@@/exports'
import { PageContainer } from '@ant-design/pro-components'
import { ActionType } from '@ant-design/pro-table'
import { message } from 'antd'
import { useRef } from 'react'

const CollectionRecord = () => {
  // 表格引用
  const tableRef = useRef<ActionType>()
  let collectionAwardState = useSnapshot(CollectionRecordModel.state)

  // 提交修改
  const onUpdateFinish = async (formData: any) => {
    let dto: CollectionRecordVO = JSON.parse(JSON.stringify(formData))
    // 字段处理

    let func = collectionAwardState.collectionRecord ? updateCollectionRecord : saveCollectionRecord
    if (await func(dto)) {
      message.success('操作成功')
      // 刷新表格
      tableRef.current?.reload()
      return true
    }
    return false
  }

  return (
    <PageContainer title="奖品领取记录">
      <CollectionRecorTable tableRef={tableRef} />

      <CollectionRecorUpdateModal onFinish={onUpdateFinish} />
    </PageContainer>
  )
}

export default CollectionRecord
